#include "rtmon.h"
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <pthread.h>
#include <math.h>

/*
 * This class check state of all agents and farther resend reply message
 *  to a jabber bot if reply was been received
 */
using namespace std;

RTMon::RTMon() : m_test_command("tst")
{
        for (int i = 0; i < m_countOfElements; i++)
        {
                m_status.push_back(new vector<bool>());
                m_count.push_back(0);
        }

}

RTMon::~RTMon()
{

}

void RTMon::start()
{
        // sleeptime ~5s per host
        m_sleep_time = floor(5000000 / m_countOfElements);
        while (1)
                for (int i = 0; i < m_countOfElements; i++)
                        checkTime(m_pool.ip->at(i), i);
}

void RTMon::checkTime(const string &ip, int &serverNum)
{
        if (!ip.empty())
        {

                vector<bool> ::const_iterator iter;
                string state;
                bool flag = false, buf, isSendHostsToBot;
                setReplyHandler(this);

                buf = request(ip, "tst", 11000);

                m_status.at(serverNum)->push_back(buf);
                m_count.at(serverNum)++;
                if (m_pool.state.at(serverNum))
                        state = "offline";
                else state = "online";
                string msg = m_pool.host->at(serverNum) + ": State was changed to " + state;
                if (m_count.at(serverNum) == m_num_of_tries)
                {
                        for (iter = m_status.at(serverNum)->begin(); iter != m_status.at(serverNum)->end(); iter++)
                                if (*iter == 1)
                                {
                                        flag = true;
                                        break;
                                }
                        if (flag != m_pool.state.at(serverNum))
                        {

                                request(*m_bot_host, msg, 11001);
                                m_pool.state.at(serverNum) = flag;
                        }
                        //send list of working hosts to bot
                        m_hosts_command = "hst";
                        isSendHostsToBot = false;
                        for (uint i = 0; i < m_countOfElements; ++i)
                        {
                                if (m_pool.state.at(i))
                                {
                                        m_hosts_command += " " + m_pool.host->at(i);
                                        isSendHostsToBot = true;
                                }
                        }
                        
                        if (isSendHostsToBot)
                                request(*m_bot_host, m_hosts_command, 11001);
                        //
                        m_count.at(serverNum) = 0;
                        m_status.at(serverNum)->clear();
                        flag = false;
                }

                usleep(m_sleep_time);

        }
}

void RTMon::reply(const std::string &msg)
{
        const std::string str = msg;
        if (!str.empty() && str != "nil")
                request(*m_bot_host, msg, 11001);

}